﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.BLL.ReceivedDocuments
{
    public class InternalDocumentReceptionCommand : IBaseCommand
    {

        #region Fields

        IList<int> _receivedDocuments;
        List<IBaseCommand> _transactionCommands;
        int _currentCommandIndex;

        #endregion

        #region Constructors

        public InternalDocumentReceptionCommand(IList<int> receivedDocuments)
        {
            _receivedDocuments = receivedDocuments;      
        }
        
        #endregion

        #region Command Methods

        public void Execute()
        {
            try
            {
                initCommands();
                for (_currentCommandIndex = 0; _currentCommandIndex < _transactionCommands.Count; _currentCommandIndex++)
                    _transactionCommands[_currentCommandIndex].Execute();
            }
            catch (Exception ex)
            {
                this.Undo();
                throw ex;
            }

        }

        public void Undo()
        {
            for (int i = _currentCommandIndex; i >= 0; i--)
                _transactionCommands[i].Undo();
        }

        #endregion

        #region Private Methods

        private void initCommands()
        { 
            _transactionCommands = new List<IBaseCommand>();
            _transactionCommands.Add(new LoadWindowReceivedDocumentsCommnad(_receivedDocuments));
            _transactionCommands.Add(new UpdatePrintedToReceivedStatusCommand(_receivedDocuments));
            _transactionCommands.Add(new DeletePrintedDocumentsCommand(_receivedDocuments));
        }

        #endregion
    }
}
